<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="菜单名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table
      :data="menus.pageList"
      highlight-current-row
      v-loading="listLoading"
      @selection-change="selsChange"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column type="index" width="60"> </el-table-column>
      <el-table-column prop="name" label="部门" width="120" sortable>
      </el-table-column>
      <el-table-column prop="url" label="url" width="180" sortable>
      </el-table-column>
      <el-table-column prop="icon" label="图标" width="180" sortable>
      </el-table-column>
      <el-table-column prop="parent.name" label="父级" width="120" sortable>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            @click="handleDel(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button
        type="danger"
        @click="batchRemove"
        :disabled="this.sels.length === 0"
        >批量删除</el-button
      >
      <el-pagination
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :current-page="query.currentPage"
        :page-size="query.pageSize"
        :total="menus.count"
        style="float: right"
      >
      </el-pagination>
    </el-col>

    <el-dialog
      title="新增"
      :visible.sync="addFormVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="addForm"
        label-width="80px"
        :rules="addFormRules"
        ref="addForm"
      >
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="url">
          <el-input v-model="addForm.url"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="addForm.icon"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单">
          <!-- <el-input v-model="addForm.manager.id" ></el-input> -->
          <el-select
            v-model="addForm.parent"
            clearable
            value-key="id"
            placeholder="请选择"
          >
            <!--下拉选项
						  :key=""  //唯一标识
						  :label 选择之后展示到选择框中的值
						  :value 选中之后绑定给模型层的值  如果要绑定对象给模型层 有一个大坑
						     必须要写  value-key="id"
						-->
            <el-option
              v-for="item in allmenu"
              :key="item.id"
              :label="item.name"
              :value="item"
            >
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click.native="addSubmit"
          :loading="addLoading"
          >提交</el-button
        >
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import {
  getUserListPage,
  removeUser,
  batchRemoveUser,
  editUser,
  addUser,
} from '../../api/api'

export default {
  data() {
    return {
      filters: {
        name: '',
      },
      menus: {
				count: 0,
				pageList: []
			},
      total: 0,
      page: 1,
      listLoading: false,
      addFormVisible:false,
      sels: [], //列表选中列

      addLoading: false,
      addFormRules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
      },
      addForm: {
        id: '',
        name: '',
        url: '',
        icon: '',
        parent: {
          id: '',
          name: ''
        }
      },
      query: {
        currentPage: 1,
        pageSize: 5,
        keyword: '',
      },
      allmenu: [],
    }
  },
  methods: {
    search(){
      this.query.currentPage = 1
      this.getMenus()
    },
    // 提交菜单
    addSubmit(index,row){
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = false
            //NProgress.start();
            let para = Object.assign({}, this.addForm)

            console.log("管理员数据",this.addForm);

            this.$http
              .put('/menu', this.addForm)
              .then((res) => {
                this.addLoading = false
                res = res.data
                if (res.success) {
                  this.$message.success('更新成功')
                  this.addFormVisible = false
                  this.search()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(() => {
                this.$message.error('网络错误')
              })
          })
        }
      })
    },
   //显示编辑界面
   handleEdit: function (index, row) {
      this.getAllParentMenu()
      this.addFormVisible = true
      this.addForm = Object.assign({}, row)
      if (!this.addForm.parent) {
        this.addForm.parent = {
          id: '',
          name: '',
        }
      }
    },
    //显示新增界面
    handleAdd: function () {
      this.getAllParentMenu()
      this.addFormVisible = true
      this.addForm= {
        id: '',
        name: '',
        url: '',
        icon: '',
        parent: {
          id: '',
          name: ''
        }
      }
    },
    getAllParentMenu(){
      this.$http.get("/menu/parentmenu").then(res=>{
        res = res.data
        if(res.success){
          console.log("***",res.data);
          this.allmenu = res.data
        }else{
          this.$message.error(res.message)
        }
      })
    },
    //性别显示转换
    formatSex: function (row, column) {
      return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知'
    },
    handleCurrentChange(val) {
      this.query.currentPage = val
      this.getMenus()
    },
    //获取菜单列表
    getMenus() {
      console.log("分页条件",this.query);
      this.$http.post('/menu', this.query).then((res) => {
        res = res.data
        console.log('数据', res.data.pageList)
        if (res.success) {
          this.menus = res.data
        } else {
          this.$message.error(res.message)
        }
      })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning',
      })
        .then(() => {
          this.listLoading = true
          //NProgress.start();
          let para = { id: row.id }
          removeUser(para).then((res) => {
            this.listLoading = false
            //NProgress.done();
            this.$message({
              message: '删除成功',
              type: 'success',
            })
            this.getMenus()
          })
        })
        .catch(() => {})
    },
    selsChange: function (sels) {
      this.sels = sels
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map((item) => item.id).toString()
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning',
      })
        .then(() => {
          this.listLoading = true
          //NProgress.start();
          let para = { ids: ids }
          batchRemoveUser(para).then((res) => {
            this.listLoading = false
            //NProgress.done();
            this.$message({
              message: '删除成功',
              type: 'success',
            })
            this.getMenus()
          })
        })
        .catch(() => {})
    },
  },
  mounted() {
    this.getMenus()
  },
}
</script>

<style scoped></style>
